/**
 * 项目名：student
 * 修改历史：
 * 作者： MZ
 * 创建时间： 2016年1月6日-下午4:56:01
 */
package com.cxy.web.swing;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.springframework.context.ApplicationContext;

import com.cxy.common.Const;
import com.cxy.entity.SysAward;
import com.cxy.service.AwardService;
import com.cxy.util.SpringContextUtils;

/**
 * 模块说明： 添加学生
 * 
 */
public class AddView extends JFrame {

	private static final long serialVersionUID = -1984182788841566838L;
	ApplicationContext ctx = SpringContextUtils.getApplicationContext();	// 获取Spring上下文对象
	private AwardService awardService = (AwardService) ctx.getBean("awardService");

	private JPanel jPanelCenter, jPanelSouth;
	private JButton addButton, exitButton;
	private JTextField name, num, level;

	public AddView() {
		init();
	}

	private void init() {
		setTitle(Const.ADDVIEW_TITLE);
		// center panel
		jPanelCenter = new JPanel();
		jPanelCenter.setLayout(new GridLayout(9, 2));
		jPanelCenter.add(new JLabel(Const.AWARD_NAME));
		name = new JTextField();
		jPanelCenter.add(name);
		jPanelCenter.add(new JLabel(Const.AWARD_NUM));
		num = new JTextField();
		jPanelCenter.add(num);
		jPanelCenter.add(new JLabel(Const.AWARD_LEVEL));
		level = new JTextField();
		jPanelCenter.add(level);

		//jPanelCenter.add(new JLabel("-------------------------------------------------"));
		//jPanelCenter.add(new JLabel("-------------------------------------------------"));

		// south panel
		jPanelSouth = new JPanel();
		jPanelSouth.setLayout(new GridLayout(1, 2));
		addButton = new JButton(Const.ADDVIEW_ADDBUTTON);
		addButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (check()) {
					SysAward award = new SysAward();
					buildAward(award);
					boolean isSuccess = awardService.add(award);
					if (isSuccess) {
						setEmpty();
						if (MainView.currPageNum < 0 || MainView.currPageNum > 99) {
							MainView.currPageNum = 1;
						}
						String[][] result = awardService.list(MainView.currPageNum);
						MainView.initJTable(MainView.jTable, result);
					}
				}
			}
		});
		jPanelSouth.add(addButton);
		exitButton = new JButton(Const.EXITBUTTON);
		exitButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		jPanelSouth.add(exitButton);

		this.add(jPanelCenter, BorderLayout.CENTER);
		this.add(jPanelSouth, BorderLayout.SOUTH);

		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setBounds(470, 200, 400, 270);
		setResizable(false);
		setVisible(true);
	}

	private boolean check() {
		boolean result = false;
		if ("".equals(name.getText()) || "".equals(num.getText()) || "".equals(level.getText())) {
			return result;
		} else {
			result = true;
		}
		return result;
	}

	private void buildAward(SysAward award) {
		award.setAwardName(name.getText());
		award.setAwardNum(Integer.valueOf(num.getText()));
		award.setAwardLevel(level.getText());
	}

	private void setEmpty() {
		name.setText("");
		num.setText("");
		level.setText("");
	}
}
